﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Snap.Extensions
{
    public static class StringExtensions
    {
        /// <summary>
        /// Converts a string to its escaped representation.
        /// </summary>
        /// <param name="dataString">The string to escape.</param>
        /// <remarks>Resolves the 32767 max character limit in System.Uri.EscapeDataString().</remarks>
        public static string EscapeDataString(this String dataString)
        {
            if (dataString == null)
                throw new ArgumentNullException("dataString");

            if (dataString.Length == 0)
                return string.Empty;

            var stringBuilder = new StringBuilder();

            for (int index = 0; index < dataString.Length; )
            {
                var substring = dataString.Substring(index, ((dataString.Length - index < Int16.MaxValue) ? (dataString.Length - index) : Int16.MaxValue));

                // Escape the data string
                stringBuilder.Append(Uri.EscapeDataString(substring));

                index += substring.Length;
            }

            return stringBuilder.ToString();
        }
    }
}
